package com.yjzx.util.common.util.number;

/**
 * @author yjzx
 * @date 2023/7/21
 * @Description NumberUtils
 */
public class NumberUtils {
    /**
     * 除法
     *
     * @param divide1    被除数
     * @param divide2    除数
     * @param divide1mul 被除数放大倍数
     * @param divide2mul 除数放大倍数
     * @return
     */
    public static int divide(Long divide1, Long divide2, Integer divide1mul, Integer divide2mul) {
        Long result;
        if (divide1 == null || divide2 == null || divide2 == 0) {
            result = 0L;
        } else {
            long div1 = divide1mul != null ? divide1 * divide1mul : divide1;
            long div2 = divide2mul != null ? divide2 * divide2mul : divide2;
            result = div1 / div2;
        }
        return result.intValue();
    }

    public static int divide(Long divide1, Long divide2) {
        Long result;
        if (divide1 == null || divide2 == null || divide2 == 0) {
            result = 0L;
        } else {
            result = divide1 / divide2;
        }
        return result.intValue();
    }

    public static int divide(Integer divide1, Integer divide2) {
        int result;
        if (divide1 == null || divide2 == null || divide2 == 0) {
            result = 0;
        } else {
            result = divide1 / divide2;
        }
        return result;
    }

    public static Long divide(boolean isNull, Long divide1, Long divide2) {
        if (isNull) {
            if (divide1 == null || divide2 == null || divide2 == 0) {
                return null;
            }
        }
        return divide1 / divide2;
    }

    public static int sub(Integer... value) {
        if (value == null || value.length < 2) {
            return value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
        } else {
            Integer num = value[0];
            int a = num == null ? 0 : num;
            for (int i = 1; i < value.length; i++) {
                Integer b = value[i];
                a -= (b == null ? 0 : b);
            }
            return a;
        }
    }

    /**
     * 减法
     *
     * @param value 对象
     * @return
     */
    public static long sub(Long... value) {

        if (value == null || value.length < 2) {
            return value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
        } else {
            Long num = value[0];
            long a = num == null ? 0 : num;
            for (int i = 1; i < value.length; i++) {
                Long b = value[i];
                a -= (b == null ? 0 : b);
            }
            return a;
        }

    }

    /**
     * 减法
     *
     * @param value 对象
     * @return
     */
    public static Long sub(boolean isNull, Long... value) {
        Long num;
        if (value == null || value.length < 2) {
            if (isNull) {
                num = value == null || value.length == 0 ? null : value[0];
            } else {
                num = value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
            }
        } else {
            num = value[0];
            if (isNull) {
                for (int i = 1; i < value.length; i++) {
                    Long a = value[i];
                    if (num != null) {
                        num = a != null ? num - a : num;
                    } else {
                        num = a != null ? -a : null;
                    }
                }
            } else {
                long a = num == null ? 0 : num;
                for (int i = 1; i < value.length; i++) {
                    Long b = value[i];
                    a -= b == null ? 0 : b;
                }
                num = a;
            }
        }
        return num;
    }

    /**
     * 加法
     *
     * @param value 对象
     * @return
     */
    public static long add(Long... value) {
        if (value == null || value.length < 2) {
            return value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
        } else {
            long num = value[0] != null ? value[0] : 0;
            for (int i = 1; i < value.length; i++) {
                Long b = value[i];
                if (b != null) {
                    num += b;
                }
            }
            return num;
        }
    }

    /**
     * 加法
     *
     * @param isNull 允许为null
     * @param value  对象
     * @return
     */
    public static Long add(boolean isNull, Long... value) {
        Long num;
        if (value == null || value.length < 2) {
            if (isNull) {
                num = value == null || value.length == 0 ? null : value[0];
            } else {
                num = value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
            }
        } else {
            num = value[0];
            if (isNull) {
                for (int i = 1; i < value.length; i++) {
                    Long a = value[i];
                    if (num != null) {
                        num = a != null ? num + a : num;
                    } else {
                        num = a;
                    }
                }
            } else {
                long a = num != null ? num : 0;
                for (int i = 1; i < value.length; i++) {
                    Long b = value[i];
                    if (b != null) {
                        a += b;
                    }
                }
                num = a;
            }
        }
        return num;
    }

    public static long add(Integer... value) {
        if (value == null || value.length < 2) {
            return value == null || value.length == 0 ? 0 : (value[0] != null ? value[0] : 0);
        } else {
            long num = value[0] != null ? value[0] : 0;
            for (int i = 1; i < value.length; i++) {
                Integer b = value[i];
                if (b != null) {
                    num += b;
                }
            }
            return num;
        }
    }

    public static Long numPadding(Long l) {
        return l != null ? l : 0L;
    }

    public static Integer numPadding(Integer l) {
        return l != null ? l : 0;
    }

}
